package cn.faurecia.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.faurecia.dto.InStockRo;
import cn.faurecia.dto.WhInStock;
import cn.faurecia.dto.WhItem;
import cn.faurecia.whinstock.service.impl.WhInstockServiceImpl;
import cn.faurecia.whitem.service.impl.WhItemServiceImpl;
import cn.faurecia.whstock.service.impl.WhStockServiceImpl;

@Controller
@RequestMapping("/instock")
public class InStockController{
  @Autowired
  private WhItemServiceImpl itemService;
  @Autowired
  private WhStockServiceImpl stockService;
  @Autowired
  private WhInstockServiceImpl instockService;
  
  @RequestMapping("/toInStock")
  public ModelAndView toInStock(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
    return new ModelAndView("/instock");
  }
  
  @RequestMapping("/scanCode")
  @ResponseBody
  public Map<String, Object> scanCode(String code,HttpServletResponse response, HttpServletRequest request, HttpSession session) {
    Map<String, Object> map = new HashMap<String, Object>();
    if(code == null || code.length() < 10){
      map.put("result","1");
      map.put("msg","条码为空或者条码长度有误");
      return map;
    }
    String codeFinal = code.substring(0, 10);
    WhItem item = new WhItem();
    item.setItemCode(codeFinal);
    List<WhItem> dataList = itemService.queryByParam(item);
    if (dataList.isEmpty()) {
      map.put("result","1");
      map.put("msg","零件号不存在");
      return map;
    }
    WhInStock instock = instockService.queryByItemCode(code);
    if(instock != null){//已入库
    	map.put("result","1");
        map.put("msg","零件已入库");
        return map;
    }
    
    map.put("result","0");
    return map;
  }
  
  @RequestMapping("/queryAll")
  @ResponseBody
  public List<WhItem> queryAll(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
    List<WhItem> dataList = itemService.queryByParam(new WhItem());
    return dataList;
  }
  
  @RequestMapping("/queryByProg")
  @ResponseBody
  public List<WhItem> queryByProg(String prog,HttpServletResponse response, HttpServletRequest request, HttpSession session) {
	WhItem item = new WhItem();
	item.setProg(prog);
    List<WhItem> dataList = itemService.queryByParam(item);
    return dataList;
  }
  
  @RequestMapping("/getItem")
  @ResponseBody
  public WhItem getItem(String code,HttpServletResponse response, HttpServletRequest request, HttpSession session) {
    if(code == null || code.length() < 10){
      return null;
    }
    String codeFinal = code.substring(0, 10);
    WhItem item = new WhItem();
    item.setItemCode(codeFinal);
    List<WhItem> dataList = itemService.queryByParam(item);
    if (dataList.isEmpty()) {
      return null;
    }else {
      return dataList.get(0);
    }
  }
  
  @RequestMapping("/instock")
  @ResponseBody
  public Map<String, Object> instock(String code0,String code1,String code2,String code3,String stock,HttpServletResponse response, HttpServletRequest request, HttpSession session) {
    Map<String, Object> map = new HashMap<String, Object>();
    String code = code0;
    if(code == null || code.length() < 10){
      map.put("result","1");
      map.put("msg","入库失败");
      return map;
    }
    String codeFinal = code.substring(0, 10);
    WhItem item = new WhItem();
    item.setItemCode(codeFinal);
    List<WhItem> dataList = itemService.queryByParam(item);
    if (dataList.isEmpty()) {
      map.put("result","1");
      map.put("msg","入库失败");
      return map;
    }else {
      return stockService.instock(dataList.get(0), code0, code1, code2, code3, stock);
    }
  }
}
